<?php    
    require_once 'config/conexao.class.php';
    require_once 'config/crud.class.php';

    $con = new conexao(); // instancia classe de conxao
    $con->connect(); // abre conexao com o banco
    
       error_reporting(E_ALL);
       ini_set('display_errors', TRUE);
       ini_set('display_startup_errors', TRUE);
       
       if (PHP_SAPI == 'cli') die('This example should only be run from a Web Browser');

       /** Include PHPExcel */
       require_once './phpexcel/Classes/PHPExcel.php';

       // Create new PHPExcel object
       $objPHPExcel = new PHPExcel();

       // Set document properties
       $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
       ->setLastModifiedBy("Maarten Balliauw")
       ->setTitle("Office 2007 XLSX Test Document")
       ->setSubject("Office 2007 XLSX Test Document")
       ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
       ->setKeywords("office 2007 openxml php")
       ->setCategory("Test result file");
       

               $objPHPExcel->setActiveSheetIndex(0)
               ->setCellValue('A1', 'ID')
               ->setCellValue('B1', 'Status')
               ->setCellValue('C1', 'Nome')
               ->setCellValue('D1', 'E-mail')
               ->setCellValue('E1', 'Empresa')
               ->setCellValue('F1', 'Plano');
               
               $index = 2;
               
               $consulta_cliente = mysql_query("SELECT * FROM ldct_cliente"); 
               
               
               while( $cliente = mysql_fetch_object($consulta_cliente) ) :
                   
                   if($cliente->clie_status==1){
                       $clie_status="ATIVO";
                   }else{
                       $clie_status="INATIVO";
                   }
                   
                   $consulta_plano = mysql_query("SELECT * FROM ldct_plano WHERE plan_id=$cliente->plan_id"); 
                    $plano = mysql_fetch_object($consulta_plano);
                   
                   // Miscellaneous glyphs, UTF-8
                   @$objPHPExcel->setActiveSheetIndex(0)
                   ->setCellValue('A'.$index, utf8_encode($cliente->clie_id) )
                   ->setCellValue('B'.$index, utf8_encode($clie_status))
                   ->setCellValue('C'.$index, utf8_encode($cliente->clie_nome) )
                   ->setCellValue('D'.$index, utf8_encode($cliente->clie_email) )
                   ->setCellValue('E'.$index, utf8_encode($cliente->clie_empresa))
                   ->setCellValue('F'.$index, utf8_encode($plano->plan_nome));

                   $index++;            
               endwhile;


       // Rename worksheet
       $objPHPExcel->getActiveSheet()->setTitle('Simple');

       // Set active sheet index to the first sheet, so Excel opens this as the first sheet
       $objPHPExcel->setActiveSheetIndex(0);


       // Redirect output to a client’s web browser (Excel2007)
       @header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
       @header('Content-Disposition: attachment;filename="'."clientes-".date("d-m-Y").'.xlsx"');
       @header('Cache-Control: max-age=0');

       $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
       $objWriter->save('php://output');
       exit;

?>